USE [AnotherSuccessfulProcedure]

IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[PlayerVictimsSelectByPlayerId]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
	DROP PROCEDURE dbo.PlayerVictimsSelectByPlayerId
END


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Neil Wilson
-- Create date: 09/03/08
-- Description:	Player Victims Select By PlayerId
-- =============================================
CREATE PROCEDURE PlayerVictimsSelectByPlayerId

			@Attacker_playerId varchar(30)
	
AS
BEGIN

DECLARE @Victim_playerId varchar(30)

DECLARE @PlayerName varchar(50),
		@Kills int,
		@Deaths int

DECLARE @tempPlayers TABLE
(
	PlayerName varchar(50),
	Kills int,
	Deaths int,
	KillDeathRatio varchar(5),
	Victim_playerId varchar(30)
)

DECLARE c1 CURSOR FAST_FORWARD
FOR
SELECT Victim_playerId from KILLS
WHERE Attacker_playerId = @Attacker_playerId
GROUP BY Victim_playerId

OPEN c1

FETCH NEXT FROM c1
INTO @Victim_playerId


WHILE @@FETCH_STATUS = 0
BEGIN

	SELECT @PlayerName = PlayerName FROM 
	(SELECT COUNT(PlayerName) AS Frequency, PlayerName, PlayerId as PlayerID 
		FROM PlayerNameList
		GROUP BY PlayerName, PlayerId ) as NewTable
	WHERE PlayerId = @Victim_playerId 
	GROUP BY PlayerName

	SELECT @Kills = Count(KillId) FROM Kills WHERE Victim_playerId = @Victim_playerId
						AND Attacker_playerId = @Attacker_playerId
	SELECT @Deaths = COUNT(KillId) FROM Kills WHERE Victim_playerId = @Attacker_playerId
						AND Attacker_playerId = @Victim_playerId

	INSERT INTO @tempPlayers VALUES (@PlayerName, @Kills, @Deaths, '', @Victim_playerId)

	FETCH NEXT FROM c1
	INTO @Victim_playerId

END

CLOSE c1
DEALLOCATE c1

SELECT PlayerName, Kills, Deaths, KillDeathRatio, Victim_playerId FROM @tempPlayers


END
GO